import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemDeptApi } from '#/api';

import { getDeptList } from '#/api';
import { $t } from '#/locales';

export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'code',
      label: $t('system.dept.code'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('system.dept.deptName'),
      rules: 'required',
    },
    {
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: getDeptList,
        class: 'w-full',
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        style: { width: '100%' },
        dropdownStyle: { maxHeight: '400px', overflow: 'auto' },
      },
      fieldName: 'parent_id',
      label: $t('system.dept.parentDept'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'status',
      label: $t('system.dept.status'),
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        max: 999,
      },
      defaultValue: 0,
      fieldName: 'order',
      label: $t('system.dept.order'),
    },
    {
      component: 'Textarea',
      fieldName: 'desc',
      label: $t('system.dept.desc'),
    },
  ];
}

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'code',
      label: $t('system.dept.code'),
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('system.dept.deptName'),
    },
  ];
}

export function useColumns<T = SystemDeptApi.Dept>(
  onActionClick?: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'name',
      title: $t('system.dept.deptName'),
      treeNode: true,
      minWidth: 180,
      resizable: true,
    },
    {
      field: 'code',
      title: $t('system.dept.code'),
      minWidth: 120,
      resizable: true,
    },
    {
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'success', label: $t('common.enabled'), value: 1 },
          { color: 'error', label: $t('common.disabled'), value: 0 },
        ],
      },
      field: 'status',
      title: $t('system.dept.status'),
      width: 80,
    },
    {
      field: 'order',
      title: $t('system.dept.order'),
      width: 70,
    },
    {
      field: 'desc',
      title: $t('system.dept.desc'),
      minWidth: 150,
      showOverflow: 'tooltip',
      resizable: true,
      visible: false, // 默认隐藏，可通过列设置显示
    },
    {
      field: 'created_at',
      title: $t('system.dept.createTime'),
      width: 160,
      formatter: 'formatDateTime',
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: $t('system.dept.name'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'append',
            text: $t('system.dept.addChild'),
            auth: 'system:systemDept:edit',
          },
          {
            code: 'edit',
            auth: 'system:systemDept:edit',
          },
          {
            code: 'delete',
            auth: 'system:systemDept:delete',
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: $t('common.operation'),
      width: 160,
    },
  ];
}
